package controller;


import dao.PoMainMapper;
import org.springframework.web.bind.annotation.*;
import pojo.*;
import response.Result;
import service.PoMainService;
import service.VenderService;
import utils.DateUtil;
import utils.IDCreateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Admin
 * @date 2022/11/18 18:32
 * @Description TODO
 * @Version 1.0
 **/
@RestController
public class PoMainController {
    @Resource
    private PoMainService service;
    @Resource
    private VenderService venderService;
    @Resource
    private PoMainMapper pojoMainMapper;

    @RequestMapping("getPomains")
    public Result getPomains(Integer currentPage, Integer pageSize, HttpServletRequest request) {
        Page<PoMain> p = new Page<PoMain>();
        p.setCurrentPage(currentPage);
        p.setPageSize(pageSize);
        PoMain po = new PoMain();
        SCMUser u = (SCMUser) request.getSession().getAttribute("loginUser");
        po.setAccount(u.getAccount());
        int totalRecord = service.getCount(po);
        List<PoMain> poMainList = service.getAllByPage(p, po);
        p.setLists(poMainList);
        p.setTotalRecord(totalRecord);
        return Result.ok(p);
    }

    //查询基础查询
    @GetMapping("getBasicMassage")
    public Result getBasicMassage(HttpServletRequest request) {
        PoMain po = new PoMain();
        SCMUser u = (SCMUser) request.getSession().getAttribute("loginUser");
        po.setAccount(u.getAccount());
        po.setCreateTime(DateUtil.getDateTime());
        po.setPoId(new BigDecimal(IDCreateUtil.getId()));
        return Result.ok(po);
    }

    //查出供应商的信息
    @GetMapping("getVenderMassage")
    public Result getVenderMassage() {
        List<Vender> venderList = venderService.getAll();
        return Result.ok(venderList);
    }

    //新增采购单
    @PostMapping("addOrUpdatePoMain")
    public Result addPoMain(@RequestBody PoMain poMain) {
        if (service.updatePomainByPoID(poMain.getPoId()) == null) {
            service.add(poMain);
        } else {
            service.del(poMain);
            service.add(poMain);
        }
        return Result.ok();
    }

    //修改采购单的页面数据回填
    @GetMapping("getPoMainByPoId")
    public Result getPoMainByPoID(String poId) {
        PoMain poMain = service.getPoMainByPoID(poId);
        return Result.ok(poMain);
    }

    @GetMapping("getPoItemsByPoId/{poId}")
    public Result getPoItemsByPoID(@PathVariable("poId") String poId) {
        List<PoItem> poItemList = service.getItemsByPoID(poId);
        return Result.ok(poItemList);
    }

    @GetMapping("delPomain")
    public Result delPomain(PoMain poId) {
        service.del(poId);
        return Result.ok();
    }

    //了结
    @RequestMapping("getPoMainPage")
    public Page<PoMain> getPoMainPage(int currentPage, int pageSize, PoMain poMain, int payType, HttpServletRequest req) {
        SCMUser scm = (SCMUser) req.getSession().getAttribute("loginUser");
        poMain.setAccount(scm.getAccount());
        Page<PoMain> PoMainPage = service.getPoMainPage(new Page<PoMain>(currentPage, pageSize, 0, null), poMain, payType);
        return PoMainPage;
    }

    //入库
    @RequestMapping("getInboundPage")
    public Page<PoMain> getInboundPage(int currentPage, int pageSize, PoMain poMain, int payType, HttpServletRequest req) {
        SCMUser sc = (SCMUser) req.getSession().getAttribute("loginUser");
        poMain.setAccount(sc.getAccount());
        Page<PoMain> InboundPage = service.inboundPage(new Page<PoMain>(currentPage, pageSize, 0, null), poMain, payType);
        return InboundPage;
    }

    //确认入库？
    @RequestMapping("updateInbound")
    public Result updateInbound(BigDecimal poId, HttpServletRequest req) {
        SCMUser u = (SCMUser) req.getSession().getAttribute("loginUser");
        PoMain poMain = new PoMain();
        poMain.setStockUser(u.getName());
        poMain.setPoId(poId);
        service.updInbound(poMain);
        return Result.ok();
    }

    //了结
    @PostMapping("updateStatus")
    public Result updateStatus(@RequestBody PoMain poMain) {
        service.endUpd(poMain);
        return Result.ok();
    }

    @RequestMapping("getPomain")
    public Result getPomain(Integer currentPage, Integer pageSize, BigDecimal poId, String createTime, String endTime, String venderCode, int payType, int status, HttpServletRequest req) {
        Page<PoMain> page = new Page<PoMain>();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        SCMUser scm = (SCMUser) req.getSession().getAttribute("loginUser");
        PoMain po = new PoMain();
        po.setPoId(poId);
        po.setCreateTime(createTime);
        po.setEndTime(endTime);
        po.setVenderCode(venderCode);
        po.setPayType(payType);
        po.setStatus(status);
        po.setAccount(scm.getAccount());
        int totalRecord = service.getPomainCount(page, po);
        List<PoMain> list = service.pomainAllByPage(page, po);
        page.setLists(list);
        page.setTotalRecord(totalRecord);
        return Result.ok(page);
    }

    //付款
    @RequestMapping("getOutMoneyPage")
    public Page<PoMain> getOutMoneyPage(int currentPage, int pageSize, PoMain poMain, int payType, HttpServletRequest req) {
        SCMUser sc = (SCMUser) req.getSession().getAttribute("loginUser");
        poMain.setAccount(sc.getAccount());
        Page<PoMain> outMoneyPage = service.outMoneyPage(new Page<PoMain>(currentPage, pageSize, 0, null), poMain, payType);
        return outMoneyPage;
    }

    @RequestMapping("updateOutMoney")
    public Result updateOutMoney(@RequestBody PoItem poitem, HttpServletRequest req) {
        SCMUser sc = (SCMUser) req.getSession().getAttribute("loginUser");
        PoMain pomain = new PoMain();
        pomain.setPayUser(sc.getName());
        pomain.setPoId(poitem.getPoId());
        Payrecord payRecord = new Payrecord();
        payRecord.setPayType(3);
        payRecord.setPayPrice(poitem.getPoTotal());
        payRecord.setAccount(sc.getName());
        payRecord.setOrdercode(String.valueOf(poitem.getPoId()));
        int paytype = poitem.getPayType();
        int status = poitem.getStatus();
        System.out.println(paytype);
        System.out.println(status);
        if (paytype == 1 || paytype == 2) {
            service.updateoutMoney1(pomain);
            payRecord.setPayType(1);
            service.addoutMoney(payRecord);
        } else if (paytype == 3) {
            if (status==2) {
                service.updateoutMoney1(pomain);
                payRecord.setPayType(1);
                float price=pomain.getPrePayFee();
                payRecord.setPayPrice(price);
                service.addoutMoney(payRecord);
            }else if(status == 1){
                service.updateoutMoney(pomain);
                payRecord.setPayType(3);
                float price=pomain.getPoTotal()-pomain.getPrePayFee();
                payRecord.setPayPrice(price);
                service.addoutMoney(payRecord);
            }
        }
//        if(poitem.getPayType()==1||poitem.getPayType()==2){
//            service.updateoutMoney(pomain);
//            service.addoutMoney(payRecord);
//        }else{
//            service.updateoutMoney1(pomain);
//            service.addoutMoney(payRecord);
//        }

        return Result.ok();
    }

    @RequestMapping("queryPoReport")
    public Result queryPoReport(PoMain pomain, String date, HttpServletRequest req) {
        Page<PoMain> page = new Page<PoMain>();
        SCMUser u = (SCMUser) req.getSession().getAttribute("loginUser");
        pomain.setAccount(u.getName());
        page.setCurrentPage(pomain.getCurrentPage());
        page.setPageSize(pomain.getPageSize());
        pomain.setDate(date);
        int totalrecord = service.poReportCount(page, pomain);
        List<PoMain> pomianList = service.poReportPage(page, pomain);
        for (PoMain p : pomianList) {
            if (p.getStatus() != 4 && p.getStatus() != 5) {
                p.setUnPay(p.getPoTotalPrice());
            }
            if (p.getStatus() == 5) {
                p.setUnPay(p.getPoTotalPrice() - p.getPrePayFee());
            }
            if (p.getStatus() == 4) {
                p.setUnPay((float) 0);
            }
        }
        page.setTotalRecord(totalrecord);
        page.setLists(pomianList);
        return Result.ok(page);
    }

    @RequestMapping("queryPoAllCount")
    public List<PoMain> queryPoAllCount(PoMain pomain, String date) {
        int i = 0;//计算了结单
        float sum = 0;
        float psum = 0;
        pomain.setDate(date);
        List<PoMain> pomianList = service.poReportPage1(pomain);
        for (PoMain poMain : pomianList) {
            if (poMain.getStatus() == 4) {
                i++;
            }
            sum += poMain.getPoTotal();
            if (poMain.getPayType() == 1) {
                if (poMain.getStatus() < 3) {
                    psum += 0;
                }
                if (poMain.getStatus() >= 3) {
                    psum += poMain.getPoTotal();
                }
            }
            if (poMain.getPayType() == 2) {
                if (poMain.getStatus() == 1) {
                    psum += 0;
                }
                if (poMain.getStatus() > 1) {
                    psum += poMain.getPoTotal();
                }
            }
            if (poMain.getPayType() == 3) {
                if (poMain.getStatus() == 1) {
                    psum += 0;
                }
                if (poMain.getStatus() == 5 || poMain.getStatus() == 2) {
                    psum += poMain.getPrePayFee();
                }
                if (poMain.getStatus() == 3 || poMain.getStatus() == 4) {
                    psum += poMain.getPoTotal();
                }
            }
        }
        int totalrecord = service.poReportCount1(pomain);
        pomain.setPoMainCount(totalrecord);
        pomain.setEndCount(i);
        pomain.setPoTotalPrice(sum);
        pomain.setPayPrice(psum);
        List<PoMain> list = new ArrayList<>();
        list.add(pomain);
        return list;
    }
}